package com.aurora.source.widget;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.aurora.source.R;
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author vondear
 * @date 2016/7/19
 * 确认 取消 Dialog
 */
public class RxThreeSelectDialog<T> extends RxDialog {

    @BindView(R.id.wv_1)
    WheelView wv1;
    @BindView(R.id.wv_2)
    WheelView wv2;
    @BindView(R.id.wv_3)
    WheelView wv3;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    ArrayWheelAdapter adapter1;
    ArrayWheelAdapter adapter2;
    ArrayWheelAdapter adapter3;
    List<T> data1 = new ArrayList<>();
    List<T> data2 = new ArrayList<>();
    List<T> data3 = new ArrayList<>();
    public void setOnItem1SelectedListener(OnItemSelectedListener onItem1SelectedListener) {
        wv1.setOnItemSelectedListener(onItem1SelectedListener);
    }

    public void setOnItem2SelectedListener(OnItemSelectedListener onItem2SelectedListener) {
        wv2.setOnItemSelectedListener(onItem2SelectedListener);
    }

    public void setOnItem3SelectedListener(OnItemSelectedListener onItem3SelectedListener) {
        wv3.setOnItemSelectedListener(onItem3SelectedListener);
    }

    public RxThreeSelectDialog(Context context, int themeResId) {
        super(context, themeResId);
        initView();
    }

    public RxThreeSelectDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initView();
    }

    public RxThreeSelectDialog(Context context) {
        super(context);
        initView();
    }

    public RxThreeSelectDialog(Activity context) {
        super(context);
        initView();
    }

    public RxThreeSelectDialog(Context context, float alpha, int gravity) {
        super(context, alpha, gravity);
        initView();
    }


    public int setData1(List<T> mdata1) {
        data1.clear();
        if(mdata1!=null){
            data1.addAll(mdata1);
        }
        wv1.setAdapter(adapter1);
        return wv1.getCurrentItem();
    }

    public int setData2(List<T> mdata2) {

        data2.clear();
        if(mdata2!=null){
            data2.addAll(mdata2);
        }
        wv2.setAdapter(adapter2);
        return wv2.getCurrentItem();
    }

    public int setData3(List<T> mdata3) {
        data3.clear();
        if(mdata3!=null){
            data3.addAll(mdata3);
        }
        wv3.setAdapter(adapter3);
        return wv3.getCurrentItem();
    }

    public void setOnSureClick(View.OnClickListener onSureClick){
        tvSure.setOnClickListener(onSureClick);
    }

    public void setOnCancelClick(View.OnClickListener onCancelClick){
        tvCancel.setOnClickListener(onCancelClick);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    private void initView() {
        View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.pop_wheel_view, null);
        ButterKnife.bind(this, dialogView);
        wv1.setCyclic(false);
        wv2.setCyclic(false);
        wv3.setCyclic(false);
        adapter1 = new ArrayWheelAdapter(data1);
        adapter2 = new ArrayWheelAdapter(data2);
        adapter3 = new ArrayWheelAdapter(data3);
        wv1.setAdapter(adapter1);
        wv1.setAdapter(adapter2);
        wv1.setAdapter(adapter3);
        setContentView(dialogView);

    }
}
